Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs

Namespace Imaging.Codecs.Dialogs
	Public Partial Class Jbig2EncoderSettingsForm
		Inherits Form

		#Region "Constructors"

		Public Sub New()
			InitializeComponent()
		End Sub

		#End Region



		#Region "Properties"

		''' <summary>
		''' Determines that existing document should be append.
		''' </summary>
		Public Property AppendExistingDocument() As Boolean
			Get
				Return appendCheckBox.Checked
			End Get
			Set
				appendCheckBox.Checked = value
			End Set
		End Property

		''' <summary>
		''' Gets or sets a value indicating whether <see cref="AppendExistingDocument"/> is enabled.
		''' </summary>
		Public Property AppendExistingDocumentEnabled() As Boolean
			Get
				Return appendCheckBox.Enabled
			End Get
			Set
				appendCheckBox.Enabled = value
			End Set
		End Property


		Private _encoderSettings As Jbig2EncoderSettings
		''' <summary>
		''' Gets or sets JBIG 2 encoder settings.
		''' </summary>
		Public Property EncoderSettings() As Jbig2EncoderSettings
			Get
				Return _encoderSettings
			End Get
			Set
				If value Is Nothing Then
					Throw New ArgumentOutOfRangeException()
				End If
				If _encoderSettings IsNot value Then
					_encoderSettings = value
					ShowEncoderSettings()
				End If
			End Set
		End Property


		#End Region



		#Region "Methods"

		Protected Overrides Sub OnLoad(e As EventArgs)
			MyBase.OnLoad(e)

			If EncoderSettings Is Nothing Then
				EncoderSettings = New Jbig2EncoderSettings()
			End If
		End Sub

		Private Sub compressionLossyCheckBox_CheckedChanged(sender As Object, e As EventArgs)
			lossyGroupBox.Enabled = useLossyCheckBox.Checked
		End Sub

		Private Sub checkBoxUseSD_CheckedChanged(sender As Object, e As EventArgs)
			symbolDictionaryGroupBox.Enabled = useSymbolDictionaryCheckBox.Checked
		End Sub

		Private Sub ShowEncoderSettings()
			If EncoderSettings.UseMmr Then
				mmrRadioButton.Checked = True
			Else
				arithmeticRadioButton.Checked = True
			End If
			useLossyCheckBox.Checked = EncoderSettings.Lossy
			inaccuracyPercentNumericUpDown.Value = EncoderSettings.Inaccuracy
			useSymbolDictionaryCheckBox.Checked = EncoderSettings.UseSymbolDictionary
			Dim symbolDictionarySize As String = EncoderSettings.SymbolDictionarySize.ToString()
			If Not sdSizeComboBox.Items.Contains(symbolDictionarySize) Then
				sdSizeComboBox.Items.Add(symbolDictionarySize)
			End If
			sdSizeComboBox.SelectedItem = symbolDictionarySize
			lossyGroupBox.Enabled = useLossyCheckBox.Checked
			symbolDictionaryGroupBox.Enabled = useSymbolDictionaryCheckBox.Checked
		End Sub

		Private Sub SetEncoderSettings()
			EncoderSettings.UseMmr = mmrRadioButton.Checked
			EncoderSettings.Lossy = useLossyCheckBox.Checked
			EncoderSettings.Inaccuracy = CInt(Math.Truncate(inaccuracyPercentNumericUpDown.Value))
			EncoderSettings.UseSymbolDictionary = useSymbolDictionaryCheckBox.Checked
			EncoderSettings.SymbolDictionarySize = Int32.Parse(DirectCast(sdSizeComboBox.SelectedItem, String))
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			SetEncoderSettings()

			DialogResult = DialogResult.OK
		End Sub

		Private Sub buttonCancel_Click(sender As Object, e As EventArgs)
			DialogResult = DialogResult.Cancel
		End Sub

		#End Region

	End Class
End Namespace
